package cn.hxzy.service.impl;

import cn.hxzy.common.Result;
import cn.hxzy.common.util.OrderUtil;
import cn.hxzy.entity.order.Waybill;
import cn.hxzy.entity.order.WaybillLog;
import cn.hxzy.entity.order.vo.OrderVo;
import cn.hxzy.entity.sourceEntity.SourceList;
import cn.hxzy.feign.clients.SourceListClient;
import cn.hxzy.mapper.WaybillMapper;
import cn.hxzy.service.WaybillLogService;
import cn.hxzy.service.WaybillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hk
 * @since 2023-06-23
 */
@Service
public class WaybillServiceImpl extends ServiceImpl<WaybillMapper, Waybill> implements WaybillService {
    @Autowired
    private SourceListClient sourceListClient;
    @Autowired
    private WaybillService waybillService;
    @Autowired
    private WaybillLogService waybillLogService;
    @Override
    public int zhuangCheUpdate(String sourceId, BigDecimal suttle,double stock) {
        Result result = sourceListClient.selectSourceById(sourceId);
        SourceList source =(SourceList) result.getData().get("source");
        double val= source.getLoadingCount()-35+ suttle.intValue();
        sourceListClient.updateLoadingCount(val,source.getSourceId());
        sourceListClient.updateGoodsHeadroom(stock,source.getSourceId());
        return 1;
    }
    @Override
    public String createWaybill(OrderVo orderVo) {
        Waybill waybill = new Waybill();
        String s = OrderUtil.YDrandom();
        waybill.setWaybillId(s);
        waybill.setSourceId(orderVo.getSourceId());
        waybill.setBillOfLadingId(OrderUtil.THrandom());
        waybill.setLicensePlateNumber(orderVo.getLicensePlateNumber());
        waybill.setDriverId(orderVo.getDriverId());
        waybill.setSigningStatus(101);
        waybill.setPaymentStatus(101);
        waybill.setWaybillStatus(101);
        waybill.setNameOfGood(orderVo.getNameOfGood());
        boolean save = waybillService.save(waybill);
        if (!save){
            return null;
        }
        return s;
    }

    @Override
    public int createQDWaybillLog(String s) {
        WaybillLog waybillLog = new WaybillLog();
        waybillLog.setOrderGrabbingTime(new Date());
        waybillLog.setLogId(OrderUtil.RZrandom());
        waybillLog.setWaybillId(s);
        boolean save = waybillLogService.save(waybillLog);
        if (!save){
            return 0;
        }
        return 1;
    }

    @Override
    public List<Waybill> selectBySourceId(String sourceId, int status) {
        return null;
    }

    @Override
    public List<Waybill> searchWaybill(String sourceId, String licensePlateNumber) {
        return null;
    }
}
